package com.wooboo.server.ext.action.ad;

import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;

import com.wooboo.dsp.redis.util.RedisClient;
import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Logger;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.action.redis.Push;
import com.wooboo.server.ext.data.ConFlow;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;


/**
 * 
 * 流量监控
 * 	计划总预算
 * 	组天预算
 * 		注意：第二天凌晨的清空天的中间累计值
 * @author wangff
 *
 */
public class MonitorBak extends ActionBase{

	public Logger logger = Logger.getLogger(MonitorBak.class);
	
	// reids存的变量key HASH类型
	private String afc = "admanage_flow_control" ;
	
	private String json;
	private int showNum ;	// 展示数
	private int clickNum ;	// 点击数
	private int groupId ;	// 组id
	private int playId ;	// 计划id
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		
		// 参数验证
		json = params.getFieldValue("json");
		if(StringUtil.isEmpty(json)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
		try {
			JSONObject jo = JSONObject.fromObject(json) ;
			groupId = Integer.parseInt(jo.get("groupId").toString()) ;
			playId = Integer.parseInt(jo.get("playId").toString()) ;
			showNum = Integer.parseInt(jo.get("showNum").toString()) ;
			clickNum = Integer.parseInt(jo.get("clickNum").toString()) ;
		} catch (Exception e) {
			throw ApiError.Type.INVALID_PARAM.toException("参数不正确") ;
		}
	}

	@Override
	public Result invokeService() throws ApiError {
		
		Result result = new Result();
		Jedis jedis = RedisClient.getRedisClient().getJedis() ;
		Push push = new Push() ;
		
		// 从reids中获取控量
		String conFlowStr = jedis.hget(afc, playId + "" + groupId) ;	// 控量
		if(conFlowStr == null) {
			throw ApiError.Type.EXIST_PARAM.toException();
		}
		ConFlow conFlow = (ConFlow)JSONObject.toBean(JSONObject.fromObject(conFlowStr), ConFlow.class) ;
		
		// 计划的总预算，停止此计划
		if(showNum + conFlow.getTotalCurShowNum() > conFlow.getTotalShowNum() || 
				clickNum + conFlow.getTotalCurClickNum() > conFlow.getTotalClickNum()) {
			Parameters params = new Parameters();
			params.addField("id", String.valueOf(playId)) ;
			params.addField("action", "plan_close") ;
			// 停止计划广告
			push.initParameters(params) ;
			push.invokeService() ;
		}
		
		// 广告组按日统计
		if(showNum + conFlow.getDayCurShowNum()> conFlow.getDayShowNum() || 
				clickNum + conFlow.getDayCurClickNum() > conFlow.getDayClickNum()) {
			// 停止这个组的广告
			Parameters params = new Parameters();
			params.addField("id", String.valueOf(groupId)) ;
			params.addField("action", "group_close") ;
			// 停止计划广告
			push.initParameters(params) ;
			push.invokeService() ;
		}
		
		// 重置reids中的当前展示量与点击量
		conFlow.setTotalCurClickNum(conFlow.getTotalCurClickNum() + clickNum) ;
		conFlow.setTotalCurShowNum(conFlow.getTotalCurShowNum() + showNum) ;
		conFlow.setDayCurClickNum(conFlow.getDayCurClickNum() + clickNum) ;
		conFlow.setDayCurShowNum(conFlow.getDayCurShowNum() + showNum) ;
		jedis.hset(afc, playId + "" + groupId, JSONObject.fromObject(conFlow).toString()) ;
		
		return result;
	}
	
	
	
	

}
